//------------------------------------------------------------------------------
// <copyright file="HeroesCollectionComponent.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System.ComponentModel;
using DotBeer.Business.Entities;
using DotBeer.Data;
using DAnton.Utilities.Extensions;
using DAnton.Utilities.Helpers;
using System.Diagnostics;
using DotBeer.Business.Components.Exceptions;
using System;

namespace DotBeer.Business.Components
{
    public static class HeroesCollectionComponent
    {
        public static void Save(HeroesCollection hColl)
        {
            AbstractDataAccess<HeroesCollection> heroesCollectionDAL = new HeroesCollectionDataAccess();
            HandleExceptions.BusinessLogicPolicy(() => heroesCollectionDAL.Save(hColl));
        }

        public static HeroesCollection Load()
        {
            AbstractDataAccess<HeroesCollection> heroesCollectionDAL = new HeroesCollectionDataAccess();
            HeroesCollection heroesCollection = null;

            HandleExceptions.BusinessLogicPolicy(() =>
                {
                    heroesCollection = heroesCollectionDAL.Load();
                });

            if (heroesCollection == null)
                heroesCollection = new HeroesCollection();

            heroesCollection.Heroes.Sort(hero => hero.Name, ListSortDirection.Ascending);

            return heroesCollection;
        }
    }
}
